package com.dynamic.cn.config;

import org.springframework.beans.BeansException;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

public class PropertiesListenerConfig {
    public static Map<String,String> propertiesMap = new HashMap<>();
    private static void processProperties(Properties props) throws BeansException{
        propertiesMap = new HashMap<String,String>();
        for (Object key :props.keySet()) {
            String keyStr = key.toString();
            try{
                //PropertiesLoaderUtils的默认编码是ISO-859-1，需转码
                propertiesMap.put(keyStr,new String(props.getProperty(keyStr).getBytes("ISO-8859-1"),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }catch (java.lang.Exception e){
                e.printStackTrace();
            }
        }
    }
    public static void loadAllProperties(String propertyFileName){
        try{
            Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
            processProperties(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String getProperty(String name){
        return propertiesMap.get(name).toString();
    }
    public static Map<String,String> getAllProperty(){
        return propertiesMap;
    }
}
